package com.moefor.clover.service;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.moefor.clover.dao.CommentDao;
import com.moefor.clover.dao.GoodsDao;
import com.moefor.clover.dao.MessageDao;
import com.moefor.clover.dao.UserDao;
import com.moefor.clover.model.Comment;
import com.moefor.clover.model.Goods;
import com.moefor.clover.model.Message;
import com.moefor.clover.model.User;
import com.moefor.clover.model.enums.StateOfComment;
import com.moefor.clover.util.MessagePackager;

@Service
public class CommunicationService {
	@Resource
	private CommentDao commentDao;
	
	@Resource
	private MessageDao messageDao;
	
	@Resource
	private GoodsDao goodsDao;
	
	@Resource
	private UserDao userDao;
	
	public MessagePackager readAllMessage(int receiverId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		messageDao.setUserReadAll(receiverId);
		msgPack.setState(true);
		return msgPack;
	}
	
	public MessagePackager getMessageByReceiver(int receiverId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		msgPack.setData(messageDao.getByReceiver(receiverId));
		if(msgPack.getData() == null){
			msgPack.setState(false);
		}else msgPack.setState(true);
		return msgPack;
	}
	
	public MessagePackager getUnreadMessage(Integer userId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		msgPack.setData(messageDao.getUnread(userId));
		if(msgPack.getData() != null){
			msgPack.setState(true);
		}
		return msgPack;
	}
	
	public MessagePackager sendComment(User author, int goodsId, String content, Integer replyId){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		User reply = null;
		if(replyId != null){
			if(replyId == author.getId()){
				msgPack.setData("请不要自娱自乐~");
				return msgPack;
			}
			reply = userDao.get(replyId);
			if(reply == null){
				msgPack.setData("回复方不存在");
				return msgPack;
			}
		}
		if(content == null || content.equals("")){
			msgPack.setData("内容不能为空");
			return msgPack;
		}
		Goods g = goodsDao.get(goodsId);
		if(g == null){
			msgPack.setData("商品不存在");
			return msgPack;
		}
		author = userDao.get(author.getId());
		Comment c = new Comment();
		c.setAuthor(author);
		c.setContent(content);
		c.setDate(new Date());
		c.setState(StateOfComment.PASSED);
		c.setReply(reply);
		c.setGoods(g);
		if(commentDao.add(c)){
			if(reply != null){
				sendMessage(author, reply.getId(), "亲,我刚在商品 <a href='item.page?goodsid="+ g.getId() + "#comment'>" + g.getName() + "</a> 的评论中回复你了咯.");
			}else{
				if(g.getOwner().getId() != author.getId())
					sendMessage(author, g.getOwner().getId(), "亲,我刚在商品 <a href='item.page?goodsid="+ g.getId() + "#comment'>" + g.getName() + "</a> 中评论了.");
			}
			msgPack.setState(true);
			msgPack.setData("评论成功!");
		}else msgPack.setData("评论失败!");
		return msgPack;
	}
	
	public MessagePackager sendMessage(User sender, int receiverId, String content){
		MessagePackager msgPack = new MessagePackager(false, null, "未知错误!");
		User receiver = userDao.get(receiverId);
		if(receiver == null){
			msgPack.setData("接收方不存在");
			return msgPack;
		}
		if(content == null || content.equals("")){
			msgPack.setData("内容不能为空");
			return msgPack;
		}
		Message msg = new Message();
		msg.setSender(sender);
		msg.setReceiver(receiver);
		msg.setContent(content);
		msg.setIsRead(false);
		msg.setDate(new Date());
		if(messageDao.add(msg)){
			msgPack.setState(true);
			msgPack.setData("发送成功!");
		}else{
			msgPack.setData("发送失败,请稍后再试!");
		}
		return msgPack;
	}
}
